// packageG/mycomponent/editMemberPopup/editMemberPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    member_item: {
      type: null,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    name: '',
    img_url: '',
    avatar:'',
    imgPath: app.globalData.imgPath,
  },
  observers: {
    'member_item': function (a) {
      console.log('aaaaaaaaaaa',a)
      this.setData({
        name: a.nickname
      })
    }
  },
  lifetimes: {
    attached() {
    },
    ready() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose(e) {
      this.setData({
        show: false
      })
      this.triggerEvent('onClose', {
        name: this.data.name,
        avatar: this.data.img_url
      })
    },
    handleTouchInput() {
      let that = this
      if (wx.getPrivacySetting) {
        wx.getPrivacySetting({
          //获取是否需要弹出
          success: res => {
            //needAuthorization 如果开发者声明了隐私收集，且用户还没同意过则返回true
            if(res.needAuthorization){
              wx.requirePrivacyAuthorize({//该接口请求隐私协议，会自动触发上边的wx.onNeedPrivacyAuthorization
                // 如果用户之前已经同意过隐私授权，会立即返回success回调，不会触发 wx.onNeedPrivacyAuthorization 事件
                success: () => {
                  that.setData({
                    focus:true
                  })
                },
                fail: (err) => {
                  console.log(err,'errrequirePrivacyAuthorize')
                  wx.navigateTo({
                    url: '/mircoApp/authorizationPage/authorizationPage',
                  })
                },
                complete: () => {}
              })
            }else{
              that.setData({
                focus:true
              })
            }
          },
          fail: () => {},
          complete: () => {}
        })
      } else {
        this.setData({
          focus: true
        })
      }
    },
    bindblurM(e){
      this.setData({
        name:e.detail.value
      })
    },
    //头像上传
    afterRead(event) {
      console.log('图片', event)
      let urlStr = app.getNetAddresss('upload.uploadPic');
      wx.uploadFile({
        url: urlStr,
        filePath: event.detail.avatarUrl,
        name: 'file',
        formData: null,
        success: (res) => {
          let _res = JSON.parse(res.data);
          console.log(_res, 'ddd')
          this.setData({
            // 'fileList[0]': {
            //   ...event.detail.avatarUrl,
            //   url: _res.data.img_url
            // },
            img_url: event.detail.avatarUrl,
            avatar: _res.data.img
          })
          console.log(event.detail.avatarUrl, 'dddddsa')
        },
      });
    },
    saveMethod() {
      let urlStr = app.getNetAddresss("member.member-info.userNicknameSave");
      app._postNetWork({
        url: urlStr,
        data: {
          avatar: this.data.avatar ? this.data.avatar : this.data.member_item.avatar,
          nickname: this.data.name ? this.data.name : '',
        },
        success:(resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1000
            });
            this.onClose();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1000
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})